package com.smart.primary.common.util;


import javax.servlet.http.HttpServletRequest;
/**
 * 
 * @author yuzhibing
 *
 */
public class CheckClientUtil {
	private static String[] mobileAgents = new String[]{"iphone","android","micromessenger"};
	public static boolean hasMoblie(HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		
		if (StringUtil.isNotEmpty(userAgent)) {
			for (int i = 0; i < mobileAgents.length; i++) {
				if(userAgent.indexOf(mobileAgents[i])>-1){
					return true;
				}
			}
		}
		return false;
	}
	
	public static boolean hasWx(HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		if (StringUtil.isNotEmpty(userAgent)) {
			if(userAgent.indexOf("micromessenger")>-1){
				return true;
			}
		}
		return false;
	}
	
	public static boolean hasIphone(HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		if (StringUtil.isNotEmpty(userAgent)) {
			if(userAgent.indexOf("iphone")>-1){
				return true;
			}
		}
		return false;
	}
	
	public static boolean hasAndroid(HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		if (StringUtil.isNotEmpty(userAgent)) {
			if(userAgent.indexOf("android")>-1){
				return true;
			}
		}
		return false;
	}
	
	
	public static String openInWebview(HttpServletRequest request) {
		String ua = request.getHeader("User-Agent").toLowerCase();
		if (ua.indexOf("micromessenger") > -1) {// 微信浏览器判断
			return "weixin";
		} else if (ua.indexOf("qq") > -1) { // QQ浏览器判断
			return "qq";
		} else if (ua.indexOf("weibo") > -1) {
			return "weibo";
		} else {
			if (ua.indexOf("android") > -1) {
				return "android";
			} else if (ua.indexOf("iphone") > -1) {
				return "ios";
			} else {
				return "other";
			}
		}
	}
		
}
